package com.itheima.sh.controller;

import com.github.pagehelper.Page;
import com.itheima.sh.pojo.User;
import com.itheima.sh.response.Result;
import com.itheima.sh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping
    public Result<User> findAll(){
        List<User> all = userService.findAll();
        return Result.okResult(all);
    }

    /**
     *
     * @RequestParam key&value key&value
     * @RequestBody 接收json或者是数组类型的
     */
    @PostMapping
    public Result add(@RequestBody User user){
        return Result.okResult(userService.add(user));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer uuid){
        return Result.okResult( userService.delete(uuid));
    }

    @GetMapping("/{id}")
    public Result<User> query(@PathVariable("id") Integer uuid){
        return Result.okResult(userService.query(uuid));
    }

    @PutMapping
    public Result<User> update(@RequestBody User user){
        return Result.okResult(userService.update(user));
    }

    @GetMapping("/search/{page}/{size}")
    public Result<User> search(@RequestBody User user,
                               @PathVariable Integer page,
                               @PathVariable Integer size){
        Page<User> userPage = userService.search(user,page,size);
        return Result.okResult(userPage);
    }
}
